// my_spk/pages/address_add/index.js
const app = getApp()
Page({
  data: {
    name: '',
    mobile: '',
    region: '',
    detail: '',
    default: false
  },
  switchChange({ detail: { value } }) {
    this.setData({ default: value })
  },
  saveAddress() {
    let add = 'push'
    if (!this.data.name || !this.data.mobile || !this.data.region || !this.data.detail) {
      return wx.utils.toast('请完善收货地址信息！')
    }
    if (this.data.default) {
      add = 'unshift'
    }
    app.address[add]({
      id: Date.now(),
      name: this.data.name,
      mobile: this.data.mobile,
      region: this.data.region,
      detail: this.data.detail,
    })
    wx.setStorageSync('address', JSON.stringify(app.address))
    wx.utils.toast('添加完成', 'success')
    wx.navigateBack()
  }
})